package com.sochinchou.res.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class Utils{
	
	private static final String TAG = "=== Utils ===";
	public static boolean isNetworkAvailable(Context context){
		Log.d(TAG, "isNetworkAvailable >>>");
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();		
		return (info != null && info.isConnected());		
	}
	
	public static boolean isGpsAvailable(Context context){
		Log.d(TAG, "isGpsAvailable >>>");
		LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);			
	}
	
	public static long parseLong(String str){
		if(str == null)
			return 0;
		
		long value = 0;
		try{
			value = Long.parseLong(str);
		}catch(NumberFormatException e){
			
		}
		return value;		
	}
	
	public static int parseInt(String str){
		if(str == null)
			return 0;
		
		int value = 0;
		try{
			value = Integer.parseInt(str);
		}catch(NumberFormatException e){
			
		}
		return value;
	}
	
	public static double parseDouble(String str){
		if(str == null)
			return 0.0;
		double value = 0;
		try{
			value = Double.parseDouble(str);
		}catch(NumberFormatException e){
			
		}
		return value;
	}
	
	public static String getParamFromPref(Context context, int type, String name) {
		Log.d(TAG, "getParamFromPref() >>>>>");
		SharedPreferences mPref = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);

		switch (type) {
		case 0:
			if (mPref.getBoolean(name, false))
				return "false";
			else
				return "true";
		case 1:
			return String.valueOf(mPref.getInt(name, 0));
		case 2:
			return mPref.getString(name, "");
		}
		
		return "";
	}
	
	public static void saveParamToPref(Context context, int type, String name, String value){
		Log.d(TAG, "saveParamToPref() >>>>>");
		SharedPreferences mPref = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
		Editor editor = mPref.edit();
		switch(type){
		case 0:
			if(value.equalsIgnoreCase("true"))
				editor.putBoolean(name, true);
			else
				editor.putBoolean(name, false);
			break;
		case 1:
			editor.putInt(name, Integer.parseInt(value));
			break;
		case 2:
			editor.putString(name, value);
		default:
			break;
		}
		
	}
	
	public static void showLongToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}

	public static void showLongToast(Context context, int res){
		Toast.makeText(context, res, Toast.LENGTH_LONG).show();
	}
	
	public static void showShortToast(Context context, int res){
		Toast.makeText(context, res, Toast.LENGTH_SHORT).show();
	}
	
	public static void showShortToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	
	public static boolean isSDCardExist(){
		Log.d(TAG, "isSDCardExist() >>>>>");
		String status = Environment.getExternalStorageState();
		return status.equals(Environment.MEDIA_MOUNTED);
	}
	
	public static File createNewFile(String filePath) {
		Log.d(TAG, "createNewFile() >>>>> (String)");
		File newFile = new File(filePath);

		if (!newFile.exists()) {
			try {
				newFile.createNewFile();
			} catch (IOException e) {
				Log.d(TAG, "catch createNewFile(String) >>>>> IOException");
			}
			
			if(newFile.exists()){
				return newFile;
			}else{
				int pos = filePath.lastIndexOf("/");
				String dirPath = filePath.substring(0, pos);
				String fileName = filePath.substring(pos + 1);
				return createNewFile(dirPath, fileName);
			}
			
		} else {
			return newFile;
		}
	}

	public static File createNewFile(File dir, String fileName) {
		Log.d(TAG, "createNewFile() >>>>> (File, String)");
		File newFile = new File(dir, fileName);

		if (!newFile.exists()) { // ------------- file not exist
			try {
				newFile.createNewFile();
			} catch (IOException e) {
				Log.d(TAG, "catch createNewFile(File, String) >>>>> IOException");
			}

			if (newFile.exists())
				return newFile;
			else
				return null;

		} else { // ---------------- file exist
			return newFile;
		}
	}

	public static File createNewFile(String dirPath, String fileName) {
		Log.d(TAG, "createNewFile() >>>>> (String, String)");
		File newFile = new File(dirPath, fileName);

		if (!newFile.exists()) { // file not exist
			try {
				newFile.createNewFile();
			} catch (IOException e) {
				Log.d(TAG, "catch createNewFile(String, String) >>>>> IOException");
			}

			if (newFile.exists()) {
				return newFile;
			} else {
				File dir = new File(dirPath);
				if (!dir.exists())
					dir.mkdirs();
				return createNewFile(dir, fileName);
			}

		} else { // file exist
			return newFile;
		}
	}
		
	public static void deleteFile(File file){
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File theFile : files)
				theFile.delete();
			file.delete();
		}else{
			file.delete();
		}
	}
	
	
	public static void copyFileChannel(File source, File target){

		try {
			FileChannel inC = new FileInputStream(source).getChannel();
			FileChannel outC = new FileOutputStream(target).getChannel();
			outC.transferFrom(inC, 0, inC.size());
			outC.close();
			inC.close();
		} catch (FileNotFoundException e) {
			Log.d(TAG, "copyFileChannel() >>>>> FileNotFoundException");
		} catch (IOException e) {
			Log.d(TAG, "copyFileChannel() >>>>> IOException");
		}
	}
	
	public static void copyDirChannel(File source, File target){
		if(!target.exists())
			target.mkdirs();
		File[] files = source.listFiles();
		for(File theFile : files){
			
			File srcFile = theFile;
			File tarFile = new File(target, theFile.getName());
			
			if(theFile.isFile())
				copyFileChannel(srcFile, tarFile);
							
			if(theFile.isDirectory())
				copyDirChannel(srcFile, tarFile);			
		}
	}
	
	public static void copyDirToDir(File source, File target){
		File src = source;
		File tar = new File(target, src.getName());
		copyDirChannel(src, tar);
	}
	
	public static void copyFileToDir(File source, File target){
		File src = source;
		File tar = new File(target, src.getName());
		if(!tar.exists()){
			
			try {
				tar.createNewFile();
			} catch (IOException e) {
				Log.d(TAG, "copyFileIntoFolder() >>>>> IOException");
			}
		}
		copyFileChannel(src, tar);
	}
	
	
	// byte[0], byte[1], byte[2], byte[3]
	public static byte[] intToByteArray(int a){
	    byte[] ret = new byte[4];
	    ret[3] = (byte) (a & 0xFF);   
	    ret[2] = (byte) ((a >> 8) & 0xFF);   
	    ret[1] = (byte) ((a >> 16) & 0xFF);   
	    ret[0] = (byte) ((a >> 24) & 0xFF);
	    return ret;
	}
	
	// byte[0], byte[1], byte[2], byte[3]
	public static int byteArrayToInt(byte[] b){
		return (b[3] & 0xFF) + ((b[2] & 0xFF) << 8) + ((b[1] & 0xFF) << 16) + ((b[0] & 0xFF) << 24);
	}
}

